//Date.java
// Declaration of the Date class.
public class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
// Constructor: Confirm proper value for month;
// call method checkDay to confirm proper
// value for day.
public Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
System.out.println( "Month " + mn +
" invalid. Set to month 1." );
}
year = yr; // could also check
day = checkDay( dy ); // validate the day
System.out.println(
"Date object constructor for date " + toString() );
}
// Utility method to confirm proper day value
// based on month and year.
private int checkDay( int testDay )
{
int daysPerMonth[] = { 0, 31, 28, 31, 30,
31, 30, 31, 31, 30,
31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for leap year
testDay == 29 &&
( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
System.out.println( "Day " + testDay +
" invalid. Set to day 1." );
return 1; // leave object in consistent state
}
// Create a String of the form month/day/year
public String toString()
{ return month + "/" + day + "/" + year; }
}
=======================================================
//Employee.java
// Declaration of the Employee class.
public class Employee extends Object {
private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;
public Employee( String fName, String lName,
int bMonth, int bDay, int bYear,
int hMonth, int hDay, int hYear)
{
firstName = fName;
lastName = lName;
birthDate = new Date( bMonth, bDay, bYear );
hireDate = new Date( hMonth, hDay, hYear );
}
public String toString()
{
return lastName + ", " + firstName +
" Hired: " + hireDate.toString() +
" Birthday: " + birthDate.toString();
}
}
//EmployeeTest.java
// Demonstrating an object with a member object.
import javax.swing.JOptionPane;
public class EmployeeTest {
public static void main( String args[] )
{
Employee e = new Employee( "Bob", "Jones", 7, 24, 1949,
3, 12, 1988 );
JOptionPane.showMessageDialog( null, e.toString(),
"Testing Class Employee",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
//ThisTest.java
// Using the this reference to refer to
// instance variables and methods.
import javax.swing.*;
import java.text.DecimalFormat;
public class ThisTest {
public static void main( String args[] )
{
SimpleTime t = new SimpleTime( 12, 30, 19 );
JOptionPane.showMessageDialog( null, t.buildString(),
"Demonstrating the \"this\" Reference",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
class SimpleTime {
private int hour, minute, second;
public SimpleTime( int hour, int minute, int second )
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String buildString()
{
return "this.toString(): " + this.toString() +
"\ntoString(): " + toString() +
"\nthis (with implicit toString() call): " +
this;
}
public String toString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return twoDigits.format( this.hour ) + ":" +
twoDigits.format( this.minute ) + ":" +
twoDigits.format( this.second );
}
}
//Time4.java
// Time4 class definition
import java.text.DecimalFormat; // used for number formatting
// This class maintains the time in 24-hour format
public class Time4 extends Object {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// Time4 constructor initializes each instance variable
// to zero. Ensures that Time object starts in a
// consistent state.
public Time4() { this.setTime( 0, 0, 0 ); }
// Time4 constructor: hour supplied, minute and second
// defaulted to 0.
public Time4( int h ) { this.setTime( h, 0, 0 ); }
// Time4 constructor: hour and minute supplied, second
// defaulted to 0.
public Time4( int h, int m ) { this.setTime( h, m, 0 ); }
// Time4 constructor: hour, minute and second supplied.
public Time4( int h, int m, int s )
{ this.setTime( h, m, s ); }
// Time4 constructor: another Time4 object supplied.
public Time4( Time4 time )
{
this.setTime( time.getHour(),
time.getMinute(),
time.getSecond() );
}
// Set Methods
// Set a new Time value using military time. Perform
// validity checks on the data. Set invalid values to zero.
public Time4 setTime( int h, int m, int s )
{
this.setHour( h ); // set the hour
this.setMinute( m ); // set the minute
this.setSecond( s ); // set the second
return this; // enables chaining
}
// set the hour
public Time4 setHour( int h )
{
this.hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
return this; // enables chaining
}
// set the minute
public Time4 setMinute( int m )
{
this.minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
return this; // enables chaining
}
// set the second
public Time4 setSecond( int s )
{
this.second = ( ( s >= 0 && s < 60 ) ? s : 0 );
return this; // enables chaining
}
// Get Methods
// get the hour
public int getHour() { return this.hour; }
// get the minute
public int getMinute() { return this.minute; }
// get the second
public int getSecond() { return this.second; }
// Convert to String in universal-time format
public String toUniversalString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return twoDigits.format( this.getHour() ) + ":" +
twoDigits.format( this.getMinute() ) + ":" +
twoDigits.format( this.getSecond() );
}
// Convert to String in standard-time format
public String toString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return ( ( this.getHour() == 12 ||
this.getHour() == 0 ) ?
12 : this.getHour() % 12 ) + ":" +
twoDigits.format( this.getMinute() ) + ":" +
twoDigits.format( this.getSecond() ) +
( this.getHour() < 12 ? " AM" : " PM" );
}
}
==============================================================
//TimeTest.java
// Chaining method calls together with the this reference
import javax.swing.*;
public class TimeTest {
public static void main( String args[] )
{
Time4 t = new Time4();
String output;
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
output = "Universal time: " + t.toUniversalString() +
"\nStandard time: " + t.toString() +
"\n\nNew standard time: " +
t.setTime( 20, 20, 20 ).toString();
JOptionPane.showMessageDialog( null, output,
"Chaining Method Calls",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
//Employee.java
// Declaration of the Employee class.
public class Employee {
private String firstName;
private String lastName;
private static int count; // # of objects in memory
public Employee( String fName, String lName )
{
firstName = fName;
lastName = lName;
++count; // increment static count of employees
System.out.println( "Employee object constructor: " +
firstName + " " + lastName );
}
protected void finalize()
{
--count; // decrement static count of employees
System.out.println( "Employee object finalizer: " +
firstName + " " + lastName +
"; count = " + count );
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public static int getCount() { return count; }
}
//EmployeeTest.java
// Test Employee class with static class variable,
// static class method, and dynamic memory.
import javax.swing.*;
public class EmployeeTest {
public static void main( String args[] )
{
String output;
output = "Employees before instantiation: " +
Employee.getCount();
Employee e1 = new Employee( "Susan", "Baker" );
Employee e2 = new Employee( "Bob", "Jones" );
output += "\n\nEmployees after instantiation: " +
"\nvia e1.getCount(): " + e1.getCount() +
"\nvia e2.getCount(): " + e2.getCount() +
"\nvia Employee.getCount(): " +
Employee.getCount();
output += "\n\nEmployee 1: " + e1.getFirstName() +
" " + e1.getLastName() +
"\nEmployee 2: " + e2.getFirstName() +
" " + e2.getLastName();
// mark objects referred to by e1 and e2
// for garbage collection
e1 = null;
e2 = null;
System.gc(); // suggest that garbage collector be called
output += "\n\nEmployees after System.gc(): " +
Employee.getCount();
JOptionPane.showMessageDialog( null, output,
"Static Members and Garbage Collection",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}